package com.ztom.daily.n2022.m4;

/**
 * 三维形体投影面积
 * <p>
 * https://leetcode-cn.com/problems/projection-area-of-3d-shapes/
 *
 * @author ZhangTao
 */
public class Day26ProjectionArea {

    public int projectionArea(int[][] grid) {
        if (grid == null || grid.length == 0 || grid[0] == null || grid[0].length == 0) {
            return 0;
        }
        int n = grid.length;
        int m = grid[0].length;

        int x = 0, y = 0, z = 0;
        for (int i = 0; i < n; i++) {
            int yh = 0, zh = 0;
            for (int j = 0; j < m; j++) {
                x += grid[i][j] > 0 ? 1 : 0;
                // y 面投影, 要看列方向的最大值
                yh = Math.max(yh, grid[j][i]);
                // z 面投影, 要看行方向的最大值
                zh = Math.max(zh, grid[i][j]);
            }
            y += yh;
            z += zh;
        }
        return x + y + z;
    }
}
